﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using WindowPlace.Common;

namespace WindowPlace
{
    public class PlaceProfile : BindableObject
    {
        private List<WindowInfo> WindowInfos_ = new List<WindowInfo>();
        public List<WindowInfo> WindowInfos
        {
            get
            {
                return WindowInfos_;
            }
            set
            {
                WindowInfos_ = value;
                base.RaisePropertyChanged("WindowInfos");
            }
        }

        private string Label_;
        public string Label
        {
            get { return Label_; }
            set
            {
                Label_ = value;
                base.RaisePropertyChanged("Label");
            }
        }

        public int Index
        {
            get
            {
                return Index_ + 1;
            }
            set
            {
                Index_ = value;
                base.RaisePropertyChanged("Index");
            }
        }
        private int Index_;

        public void PlaceWindows()
        {
            //Debug.Print("PlaceProfile::PlaceWindows called");
            if (this.WindowInfos_.Count > 0)
            {
                foreach (WindowInfo wi in this.WindowInfos_.Where(a => a.IgnorePlacementAndSize == false))
                {
                    bool result = Common.Helper.SetWindowRectangle(wi.hWnd, wi.CurrentRectangle);
                    result = Common.Helper.ShowWindow(wi.hWnd, WindowShowStyle.Restore);
                    //Debug.Print("   PlaceWindow '{0}' (CaptionFilter = {1}) to {2} {3} was ok = {4}", wi.Caption, wi.CaptionFilter, wi.LocationString, wi.SizeString, result);
                }
            }
        }

        public bool IsValid
        {
            get
            {
                bool result = true;
                if (WindowInfos_.Count > 0)
                {
                    result = !WindowInfos_.Any(a => string.IsNullOrWhiteSpace(a.CaptionFilter));
                }
                return result;
            }
        }
    }
}
